package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.adapter.CreditsAdapter;
import com.warrantyhotel.android.billing.BuyCredits;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.model.BalanceObject;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.service.UpdateIntentService;
import com.warrantyhotel.android.util.DetachableResultReceiver;
import com.warrantyhotel.android.util.DetachableResultReceiver.Receiver;
import com.warrantyhotel.android.util.JSONParser;

public class GetCreditsActivity extends Activity implements OnItemClickListener, Receiver {
	private DetachableResultReceiver receiver;
	private ListView list;
	private SharedPreferences prefs;
	private ListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_credits);

		prefs = PreferenceManager.getDefaultSharedPreferences(this);

		list = (ListView) findViewById(R.id.list_credit_actions);
		adapter = new CreditsAdapter(getApplicationContext(), R.layout.get_credit_item, new String[2]);
		list.setAdapter(adapter);
		list.setOnItemClickListener(this);

		receiver = new DetachableResultReceiver(new Handler());
		receiver.setReceiver(this);

		Intent updateBalance = new Intent(Intent.ACTION_SYNC, null, this, UpdateIntentService.class);
		updateBalance.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
		updateBalance.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.BALANCE);
		startService(updateBalance);

//		takeProfit("010c005u_v1.0_android", 999);
		
	}

//	private void takeProfit(String productId, int orderId) {
//		Intent loginIntent = new Intent(Intent.ACTION_SYNC, null, this, UpdateIntentService.class);
//		loginIntent.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
//		loginIntent.putExtra(Keys.InAppProductID, productId);
//		loginIntent.putExtra(Keys.requestId, orderId);
//		loginIntent.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.SUBMIT_PURCHASE);
//		startService(loginIntent);
//	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		Intent intent = new Intent();
		switch (position) {
//		case 0:
//			intent.setClass(getApplicationContext(), FreeCredits.class);
//			break;
		case 0:
			intent.setClass(getApplicationContext(), BuyCredits.class);
			break;
		case 1:
			return;
		default:
			break;
		}

		startActivity(intent);

	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case UpdateIntentService.STATUS_RUNNING:
			break;
		case UpdateIntentService.STATUS_ERROR:
			break;
		case UpdateIntentService.STATUS_FINISHED:

			if (resultData.containsKey(Keys.balance)) {
				BalanceObject ba = resultData.getParcelable(Keys.balance);

				final Editor edit = prefs.edit();
				edit.putString(JSONParser.REMAINING_CREDITS, ba.getRemainingCredits());
				edit.putString(JSONParser.REMAINING_UPLOADS, ba.getRemainingUploads());
				edit.commit();

				adapter = new CreditsAdapter(getApplicationContext(), R.layout.get_credit_item, new String[2]);
				list.setAdapter(adapter);

			}

			break;
		}

	}

}